package com.xr.blog.controller;

import com.xr.blog.pojo.BlogTag;
import com.xr.blog.service.BlogTagService;
import com.xr.blog.tools.Result;
import com.xr.blog.tools.constant.Code;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 标签表
 *
 * @author XRJ
 * @email 1033835209@qq.com
 * @date 2020-07-07 20:31:38
 */
@RestController
@RequestMapping("/squirrelblog")
public class BlogTagController {
    @Autowired
    private BlogTagService blogTagService;

    @GetMapping(value = "/tag/list",produces = "application/json;charset=UTF-8")
    public Result<List<BlogTag>> list() {
        return Result.of(Code.SC_OK.getState(),"查询成功",blogTagService.list());
    }

    @PostMapping(value = "/tag/addOrUpdate",produces = "application/json;charset=UTF-8")
    public Result save(@RequestBody @Validated({Update.class, Insert.class}) BlogTag tag){
        blogTagService.saveOrUpdate(tag);
        return Result.of(Code.SC_OK.getState(),"操作成功",tag);
    }

    @GetMapping(value = "/tag/getById",produces = "application/json;charset=UTF-8")
    public Result<BlogTag> info(Long id){
        return Result.of(Code.SC_OK.getState(),"查询成功",blogTagService.getById(id));
    }

    @DeleteMapping(value = "/tag/delete",produces = "application/json;charset=UTF-8")
    public Result remove(Long id){
        return blogTagService.removeById(id)?Result.of(Code.SC_OK.getState(),"删除成功",null):Result.of(Code.SC_OK.getState(),"删除失败",null);
    }

}
